#include "StatusBar.h"

#include <auth/login/auth_login.h>
#include <core/StyleMgr.h>
#include <widgets/SImageButton.h>

#include <QAction>
#include <QBoxLayout>
#include <QDateTime>
#include <QDebug>
#include <QLabel>
#include <QLibrary>
#include <QMenuBar>
#include <QMouseEvent>
#include <QPushButton>
#include <QSlider>
#include <QTimer>

#include "MainFrame.h"

StatusBar::StatusBar(MainFrame* frame, QWidget* parent /* = nullptr*/)
    : QWidget(parent), m_tmSysTime(nullptr), m_frame(frame) {
  initialize();
}

StatusBar::~StatusBar() {}

void StatusBar::initialize() {
  setAutoFillBackground(true);
  QPalette palette = this->palette();
  palette.setColor(QPalette::Window, NAME_COLOR("MainTitleBack"));
  setPalette(palette);

  QHBoxLayout* layout = new QHBoxLayout(this);
  layout->setContentsMargins(10, 0, 10, 0);
  layout->setSpacing(8);
  layout->addStretch();

  // 系统时间
  QLabel* systime = new QLabel(this);
  systime->setContentsMargins(10, 0, 10, 0);
  layout->addWidget(systime, 0, Qt::AlignVCenter);
  m_tmSysTime = new QTimer(this);
  m_tmSysTime->callOnTimeout(this, [systime, this]() {
    QColor color = NAME_COLOR("LabelNormal");
    QRgb txtRgb = qRgb(color.red(), color.green(), color.blue());
    QString txtRgbStr = QString::number(txtRgb, 16);
    static QString title = "<p style=\"font-size:%1px; color:#%2\">%3</p>";
    systime->setText(title.arg(12).arg(txtRgbStr).arg(getSystemTime()));
  });
  m_tmSysTime->setInterval(1000);
  m_tmSysTime->start();

  setMouseTracking(true);
}

QString StatusBar::getSystemTime() {
  return QDateTime::currentDateTime().toString("MM/dd hh:mm:ss");
}

void StatusBar::addBtn(QWidget* btn, QWidget* before) {
  QHBoxLayout* lay = static_cast<QHBoxLayout*>(layout());
  lay->addWidget(btn, 0, Qt::AlignVCenter);
}
